package com.aznest.service.impl;

import com.aznest.mapper.HotelMapper;
import com.aznest.mapper.OrdersMapper;
import com.aznest.mapper.TypeMapper;
import com.aznest.mapper.UserMapper;
import com.aznest.service.StatAllService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
@Slf4j
public class StatAllServiceImpl implements StatAllService {
    @Resource
    private HotelMapper hotelMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private TypeMapper typeMapper;
    @Resource
    private OrdersMapper ordersMapper;

    /**
     * 统计基础数据
     * @return
     */
    @Override
    public Map<String, Object> base() {
        Map<String, Object> resultMap = new HashMap<>();
        // 统计酒店数量
        Integer hotelNum = hotelMapper.selectHotelCount();
        // 统计用户数量
        Integer userNum = userMapper.selectUserCount();
        // 统计平均房价
        Double avgPrice = typeMapper.selectAvgPrice();
        // 统计最热门酒店: 订单数最多的酒店
        List<Map<String,Object>> hotelOrderNum = ordersMapper.selectHotelAndOrdersNum();
        String hotHotelName = hotelOrderNum.get(0).get("hotelName").toString();

        // 组织数据
        resultMap.put("hotelNum", hotelNum);
        resultMap.put("userNum", userNum);
        resultMap.put("avgPrice", avgPrice);
        resultMap.put("hotHotel", hotHotelName);

        return resultMap;
    }

    /**
     * 统计订单数前五的酒店
     * @return
     */
    @Override
    public Map<String, Object> loadHotelOrderNumTopFive() {
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String,Object>> data = new ArrayList<>();
        // 查询订单数前五的酒店
        List<Map<String, Object>> hotelAndOrdersNum = ordersMapper.selectHotelAndOrdersNum();
        // 封装data
        for (Map<String,Object> map : hotelAndOrdersNum) {
            Map<String,Object> item = new HashMap<>();
            item.put("name", map.get("hotelName"));
            item.put("value", map.get("orderCount"));
            data.add(item);
        }

        // 组织数据
        resultMap.put("title","酒店订单数前五统计图（饼图）");
        resultMap.put("subtext","统计维度：每家酒店订单数");
        resultMap.put("name","订单数");
        resultMap.put("data",data);

        return resultMap;
    }


    /**
     * 统计本月营业额前五的酒店
     * @return
     */
    @Override
    public Map<String, Object> loadHotelIncomeTopFive() {
        Map<String, Object> resultMap = new HashMap<>();
        List<String> xData = new ArrayList<>();
        List<Double> data = new ArrayList<>();
        // 查询本月营业额前五的酒店
        List<Map<String,Object>> res = ordersMapper.selectHotelIncomeTopFive();
        for (Map<String,Object> map : res) {
            xData.add(map.get("hotelName").toString());
            data.add(Double.parseDouble(map.get("revenue").toString()));
        }

        // 组织数据
        resultMap.put("text","酒店本月营业额前五统计图（柱状图）");
        resultMap.put("subtext","统计维度：每家酒店本月收入");
        resultMap.put("xData",xData);
        resultMap.put("data",data);
        return resultMap;
    }

}
